#include "cell_main.h"
#include "ui_cell_main.h"

Cell_Main::Cell_Main(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Cell_Main)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    //m_titleRect = ui->title->geometry();

    connect(&m_socket, &QAbstractSocket::connected, this,[&]()
            {
                auto ip = m_socket.localAddress().toString();
                ui->textBrowser->append(ip + "连接成功");
            });

    connect(&m_socket, &QAbstractSocket::disconnected, this,[&]()
            {
                auto ip = m_socket.localAddress().toString();
                ui->textBrowser->append(ip + "断开连接");

            });

    connect(&m_socket, &QAbstractSocket::readyRead, this, &Cell_Main::dealClientRead);
    //tcp_connect();
    connect(&m_dlg, &Dlg_Setting::cnt, this, &Cell_Main::tcp_connect);

    ui->lb_name->setText(m_dlg.getName());
}

Cell_Main::~Cell_Main()
{
    delete ui;
}

void Cell_Main::mousePressEvent(QMouseEvent *event)
{
    auto rect = ui->title->geometry();
    auto p = event->pos();
    if(rect.contains(p))
    {
        m_isPressed = true;
        m_winRect = this->geometry().topLeft();
        m_pressPoint = this->mapToGlobal(p);
    }
}

void Cell_Main::mouseReleaseEvent(QMouseEvent *event)
{
    m_isPressed = false;

}

void Cell_Main::mouseMoveEvent(QMouseEvent *event)
{
    if(m_isPressed)
    {
        auto v = mapToGlobal(event->pos()) - m_pressPoint;
        auto rect =m_winRect;
        this->move(v+rect);
    }

}

void Cell_Main::tcp_connect()
{
    auto local_ip = m_dlg.getLocalIP();
    auto server_ip = m_dlg.getServerIP();
    auto server_port = m_dlg.getServerPort();

    m_socket.close();

    m_socket.bind(QHostAddress(local_ip));
    m_socket.connectToHost(server_ip, server_port);
    ui->lb_name->setText(m_dlg.getName());
}

void Cell_Main::dealClientRead()
{
    m_buffer.push_back(m_socket.readAll());

    if(m_buffer[0] == 0x66 && m_buffer.size() >= 4)
    {
        quint16 len = (m_buffer[2]<<8 & 0xFF00)|static_cast<quint16>(m_buffer[1]);

        if(m_buffer.size() >= len)
        {
            QByteArray arr = m_buffer.mid(0, len);
            dealPack(arr);
            m_buffer = m_buffer.mid(len);
        }
    }
}

void Cell_Main::on_btn_set_clicked()
{
    //Dlg_Setting dlg;
    m_dlg.exec();
    //qDebug()<<
}


void Cell_Main::on_btn_close_clicked()
{
    exit(0);
}


void Cell_Main::on_btn_min_clicked()
{
    showMinimized();
}


void Cell_Main::on_btn_max_clicked()
{
    showMaximized();
}


void Cell_Main::on_btn_send_clicked()
{
    auto str = ui->textEdit->toPlainText();
    ui->textEdit->clear();

    QByteArray arr = str.toUtf8();
    quint16 l = arr.size()+4;
    arr.insert(0, 0x66);
    arr.insert(1, static_cast<char>(l&0x00FF));
    arr.insert(2, static_cast<char>(l>>8&0x00FF));
    arr.insert(3, 0x03);
    //dealPack(arr);
    m_socket.write(arr);

}


void Cell_Main::on_btn_clear_clicked()
{
    ui->textBrowser->clear();
}

void Cell_Main::dealPack(QByteArray arr)
{
    char cmd = arr[3];
    QByteArray arrData = arr.mid(4);

    do
    {
        if(cmd == 0x01)
        {
            //心跳反馈

            break;
        }
        auto strTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
        if(cmd == 0x02)
        {
            //发送的数据包
            ui->textBrowser->append("<font color='green'>" + strTime +"我发送的：" + arrData);
            break;
        }
        if(cmd == 0x03)
        {
            //接收的数据包

            ui->textBrowser->append("<font color='blue'>" + strTime +"我接收的：" + arrData);
            break;

        }
    }while(false);


}

